#import "SEGTabBarItemView.h"
@implementation SEGTabBarItemViewLayout
- (instancetype)initWithData:(SEGMenuListModel *)data type:(SEGEnumTabBarItemViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segTabBarItemViewLayout];
    return self;
}

-(void)segTabBarItemViewLayout{
    //高度固定
    if (_type == SEGEnumTabBarItemViewType_NO ||
        _type == SEGEnumTabBarItemViewType_FreshItem ||
        _type == SEGEnumTabBarItemViewType_OPENDOOR ||
        _type == SEGEnumTabBarItemViewType_PIC) {
        _totalHeight = LYTabBarHeight();
    }
    if (_type == SEGEnumTabBarItemViewType_TOPOPENDOOR) {
        _totalHeight = LYTabBarHeight() + kTabBarItemOverHeight;
    }
    
    //高度不固定
    //_totalHeight = 0;
    _contentLabelHeight = 0;
    _contentLabelTextLayout = nil;
    _contentLabelTextLayoutPre = nil;
    _contentLabelTextLayoutFresh = nil;
    if (!_data) return;
    
    [self layout];
}

-(SEGTextLayout*)segContentLayoutWithFontSize:(CGFloat)size color:(UIColor*)color content:(NSString*)content{
    if (!content)return nil;
    //内容
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:size];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 0.5;
    modifier.paddingBottom = 0;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth() , HUGE);//* 0.298
    container.maximumNumberOfRows = 1;
    container.linePositionModifier = modifier;

    SEGTextLayout *t = [SEGTextLayout layoutWithContainer:container text:text];
    _contentLabelHeight = [modifier heightForLineCount:t.rowCount];
    return t;
}
//数据处理
-(void)layout{
    if (_type == SEGEnumTabBarItemViewType_NO||
        _type == SEGEnumTabBarItemViewType_OPENDOOR||
        _type == SEGEnumTabBarItemViewType_FreshItem) {
 
        UIColor *color = kColor_3;
        UIColor *preColor = kColor_1;
        _contentLabelTextLayout = [self segContentLayoutWithFontSize:10 color:color content:_data.serviceName];
        _contentLabelTextLayoutPre = [self segContentLayoutWithFontSize:10 color:preColor content:_data.serviceName];

        _contentLabelTextLayoutFresh = [self segContentLayoutWithFontSize:10 color:kColor_1 content:@"刷新"];
    }
    if (_type == SEGEnumTabBarItemViewType_TOPOPENDOOR) {
        _contentLabelTextLayout = [self segContentLayoutWithFontSize:10 color:kColor_3 content:_data.serviceName];
    }
}
@end

@implementation SEGTabBarItemView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kTabBarItemViewHeight;
    }
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor clearColor];
    
    [self addSubview:self.lineView];
    [self addSubview:self.backgroundView];
    [self addSubview:self.contentLabel];
    [self addSubview:self.headImageView];
    [self addSubview:self.redImageView];
    [self addSubview:self.freshImageView];
    
    return self;
}

-(void)setWithLayout:(SEGTabBarItemViewLayout *)layout{
    self.height = layout.totalHeight;
    _layout = layout;

    if (self.height <= 0) return;
    
    [self setSelected:NO];

    
    self.backgroundView.frame = CGRectMake(0, CGRectGetHeight(self.frame)-LYTabBarHeight(), CGRectGetWidth(self.frame), LYTabBarHeight());
    self.lineView.frame = CGRectMake(0, CGRectGetMinY(self.backgroundView.frame), CGRectGetWidth(self.frame), 0.5);
    
    if (_layout.type == SEGEnumTabBarItemViewType_NO||_layout.type == SEGEnumTabBarItemViewType_FreshItem) {
        self.contentLabel.hidden = NO;
        [self setupUI];
    }else if (_layout.type == SEGEnumTabBarItemViewType_OPENDOOR) {
        self.contentLabel.hidden = NO;
        self.redImageView.hidden = YES;
        [self setupNormalOpenDoor];
    }else if (_layout.type == SEGEnumTabBarItemViewType_PIC) {
        self.contentLabel.hidden = YES;
        self.redImageView.hidden = YES;
        [self setupOpenDoor];
    }else if (_layout.type == SEGEnumTabBarItemViewType_TOPOPENDOOR) {
        self.redImageView.hidden = YES;
        self.lineView.frame = CGRectMake(0,  kTabBarItemOverHeight , CGRectGetWidth(self.frame), 0.5);
        [self setupTopDoor];
    }
    
    self.lineView.frame = CGRectMake(0, CGRectGetMinY(self.backgroundView.frame)-0.5, CGRectGetWidth(self.frame), 0.5);
    self.lineView.backgroundColor = kColor_L2;
}
-(NSString*)segPlaceholderImageName{
    if ([_layout.data.action isEqualToString:@"TAB_SHARE"])return @"icon_tab_home_nor";
    if ([_layout.data.action isEqualToString:@"TAB_SERVICE"])return @"icon_tab_service_nor";
    if ([_layout.data.action isEqualToString:@"TAB_DOOR"])return @"icon_tab_door";
    if ([_layout.data.action isEqualToString:@"TAB_MESSAGE"])return @"icon_tab_message_nor";
    if ([_layout.data.action isEqualToString:@"TAB_ME"])return @"icon_tab_me_nor";
    if ([_layout.data.action isEqualToString:@"TAB_PROPERTY"])return @"icon_tab_service_nor";
    if ([_layout.data.action isEqualToString:@"TAB_COMMUNITY"])return @"icon_tab_linli_n";
    if ([_layout.data.action isEqualToString:@"TAB_WEB"])return @"icon_tab_web_nor";
    return @"pic_default_300_300";
}

-(NSString*)segPlaceholderImageNamePre{
    if ([_layout.data.action isEqualToString:@"TAB_SHARE"])return @"icon_tab_home";
    if ([_layout.data.action isEqualToString:@"TAB_SERVICE"])return @"icon_tab_service_pre";
    if ([_layout.data.action isEqualToString:@"TAB_DOOR"])return @"icon_tab_door";
    if ([_layout.data.action isEqualToString:@"TAB_MESSAGE"])return @"icon_tab_message_pre";
    if ([_layout.data.action isEqualToString:@"TAB_ME"])return @"icon_tab_me_pre";
    if ([_layout.data.action isEqualToString:@"TAB_PROPERTY"])return @"icon_tab_service_pre";
    if ([_layout.data.action isEqualToString:@"TAB_COMMUNITY"])return @"icon_tab_linli_p";
    if ([_layout.data.action isEqualToString:@"TAB_WEB"])return @"icon_tab_web_pre";
    return @"pic_default_300_300";
}

-(void)setSelected:(BOOL)isSelected{
    
    BOOL isDark = NO;
    if (@available(iOS 12.0, *)) isDark = (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark);
    NSString *iconString = [_layout.data darkIconWithIsDark:isDark];
    NSString *iconPreString = [_layout.data darkPreIconWithIsDark:isDark];
    
    NSString *urlString = [NSString stringWithFormat:@"%@%@",kClientImageRootURL, isSelected ?_layout.data.iosiconpre : _layout.data.iosicon];
    NSString *urlDarkString = [NSString stringWithFormat:@"%@%@",kClientImageRootURL, isSelected ?iconPreString : iconString];
    NSString * placeholder = isSelected ? [self segPlaceholderImageNamePre]:[self segPlaceholderImageName];
    UIImage *placeholderImage = [UIImage imageWithContentsOfFile:SEGPicName(placeholder)];
    
    [self.headImageView seg_setImageWithURL:[NSURL URLWithString:urlString] darkImageWithURL:[NSURL URLWithString:urlDarkString] placeholderImage:placeholderImage];
    
    //刷新图标 根据后台配置
    UIImage *freshImage = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_tab_refresh")];
    [self.freshImageView seg_setImageWithURL:[NSURL URLWithString:urlString] darkImageWithURL:[NSURL URLWithString:urlDarkString] placeholderImage:freshImage];
    //点击字体高亮
    if (_layout.type == SEGEnumTabBarItemViewType_NO|| _layout.type == SEGEnumTabBarItemViewType_FreshItem) {
        self.contentLabel.textLayout = isSelected ? _layout.contentLabelTextLayoutPre : _layout.contentLabelTextLayout;
    }
    
    if (self.freshImageView.hidden) {
        if (!self.contentLabel.textLayout || [self.contentLabel.textLayout isEqual:_layout.contentLabelTextLayoutFresh]) {
            self.contentLabel.textLayout = _layout.contentLabelTextLayout;
        }
    }
    self.isSelected = isSelected;
}

/**
 * 判断刷新图标是否显示或者隐藏
 * @author Sam
 */
-(void)segFreshImageShow:(BOOL)isShow{
    if(_layout.type != SEGEnumTabBarItemViewType_FreshItem)return;
    self.freshImageView.hidden = isShow ? NO : YES;
    self.headImageView.hidden = isShow ? YES : NO;
    if (isShow && [self.contentLabel.textLayout.text.string isEqualToString:_layout.contentLabelTextLayout.text.string]) {
        self.contentLabel.textLayout = _layout.contentLabelTextLayoutFresh;
    }
    if (self.freshImageView.hidden && ![self.contentLabel.textLayout isEqual:_layout.contentLabelTextLayout]) {
        self.contentLabel.textLayout = _layout.contentLabelTextLayout;
    }
}

//界面布局
-(void)setupUI{
    CGFloat bottomPadding = 0;
    if (LYIsiPhoneX()) {
        bottomPadding = LYSafeAreaBottom() / 2;
    }
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headImageView.superview);
        make.bottom.equalTo(self.headImageView.superview.mas_centerY).mas_offset(4-bottomPadding);
        make.width.height.mas_equalTo(21);
    }];
    
    [self.freshImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headImageView.superview);
        make.bottom.equalTo(self.headImageView.superview.mas_centerY).mas_offset(4-bottomPadding);
        make.width.height.mas_equalTo(21);
    }];
    
    [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentLabel.superview);
        make.top.equalTo(self.headImageView.mas_bottom).mas_offset(4);
        make.width.lessThanOrEqualTo(self.contentLabel.superview.mas_width);
        make.height.mas_equalTo(self.layout.contentLabelHeight);
    }];

    [self.redImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headImageView.mas_right).mas_offset(-1);
        make.centerY.equalTo(self.headImageView.mas_top).mas_offset(2);
        make.width.height.mas_equalTo(8);
    }];
}

/**
 * 开门按钮界面
 * @author Sam
 */
-(void)setupOpenDoor{
    CGFloat bottomPadding = 0;
    if (LYIsiPhoneX()) {
        bottomPadding = LYSafeAreaBottom() / 2;
    }
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headImageView.superview);
        make.centerY.equalTo(self.headImageView.superview).mas_offset(-bottomPadding);
        make.width.height.mas_equalTo(41);
     }];
}

-(void)setupTopDoor{
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headImageView.superview);
        make.top.equalTo(self.headImageView.superview);
        make.width.height.mas_equalTo(40);
    }];
    
    [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentLabel.superview);
        make.bottom.equalTo(self.headImageView.superview.mas_bottom).mas_offset(-5-LYSafeAreaBottom());
        make.width.lessThanOrEqualTo(self.contentLabel.superview.mas_width);
        make.height.mas_equalTo(self.layout.contentLabelHeight);
    }];
}

-(void)setupNormalOpenDoor{
    CGFloat bottomPadding = 0;
    if (LYIsiPhoneX()) {
        bottomPadding = LYSafeAreaBottom() / 2;
    }
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headImageView.superview);
        make.bottom.equalTo(self.headImageView.superview.mas_centerY).mas_offset(4-bottomPadding);
        make.width.height.mas_equalTo(21);
    }];
    
    [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentLabel.superview);
        make.top.equalTo(self.headImageView.mas_bottom).mas_offset(4);
        make.width.lessThanOrEqualTo(self.contentLabel.superview.mas_width);
        make.height.mas_equalTo(self.layout.contentLabelHeight);
    }];
}

#pragma mark - getter
-(SEGLabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[SEGLabel alloc]init];
        _contentLabel.userInteractionEnabled = NO;
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        //_contentLabel.displaysAsynchronously = YES;
        _contentLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _contentLabel.fadeOnHighlight = NO;
        _contentLabel.numberOfLines = 0;
        _contentLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentLabel.fadeOnAsynchronouslyDisplay = NO;
    }
    return _contentLabel;
}

-(UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc]init];
        _headImageView.contentMode = UIViewContentModeScaleAspectFit;
        _headImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")];
    }
    return _headImageView;
}

-(UIImageView *)redImageView{
    if (!_redImageView) {
        _redImageView = [[UIImageView alloc]init];
        _redImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"tab_red_round")];
        _redImageView.contentMode = UIViewContentModeScaleAspectFill;
        _redImageView.hidden = YES;
    }
    return _redImageView;
}

-(UIImageView *)freshImageView{
    if (!_freshImageView) {
        _freshImageView = [[UIImageView alloc]init];
        _freshImageView.hidden = YES;
        _freshImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_tab_refresh")];
        _freshImageView.contentMode = UIViewContentModeScaleAspectFill;
        _freshImageView.layer.masksToBounds = YES;
        if (kDebug)_freshImageView.backgroundColor = kSegRandomColor;
    }
    return _freshImageView;
}

-(UIView *)lineView{
    if (!_lineView){
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = kColor_L2;
    }
    return _lineView;
}

-(UIView *)backgroundView{
    if (!_backgroundView){
        _backgroundView = [[UIView alloc]init];
        _backgroundView.backgroundColor = kColor_W;
    }
    return _backgroundView;
}
@end

